package com.xhsj.user.hr.process.hrInterviewRegistration.service;

import com.google.common.collect.Lists;
import com.xhsj.user.hr.process.dto.PageDto;
import com.xhsj.user.hr.process.dto.hrInterviewRegistration.SaveInterviewRegistrationDto;
import com.xhsj.user.hr.process.dto.hrInterviewRegistration.ShowInterviewRegistrationDto;
import com.xhsj.user.hr.process.dto.hrInterviewRegistration.TbHrInterviewRegistrationLanguageDto;
import com.xhsj.user.hr.process.hrInterviewRegistration.entity.*;
import com.xhsj.user.hr.process.hrInterviewRegistration.mapper.TbHrInterviewRegistrationMapper;
import com.xhsj.user.hr.process.hrInterviewRegistration.query.ShowInterviewRegistrationDetailQuery;
import com.xhsj.user.hr.process.hrInterviewRegistration.query.ShowInterviewRegistrationQuery;
import com.xhsj.user.hr.base.BaseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.hr.process.hrInterviewRegistration.query.TbHrInterviewRegistrationLanguageQuery;
import com.xhsj.user.hr.system.vo.MonthCountVo;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * hr面试记录登记表 服务实现类
 * </p>
 *
 * @author liuqinghua
 * @since Sep 4, 2020 11:00:58 AM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbHrInterviewRegistration")
public class TbHrInterviewRegistrationService extends BaseService<TbHrInterviewRegistrationMapper, TbHrInterviewRegistration>{
    @Autowired
    private TbHrInterviewRegistrationMapper dao;

    // hr面试登记表-教育经历 服务实现类
    @Autowired
    private TbHrInterviewRegistrationEducationService tbHrInterviewRegistrationEducationService;

    // hr面试登记表-家庭成员 服务实现类
    @Autowired
    private TbHrInterviewRegistrationFamilyService tbHrInterviewRegistrationFamilyService;

    // hr面试登记表-文件 服务实现类
    @Autowired
    private TbHrInterviewRegistrationFileService tbHrInterviewRegistrationFileService;

    // hr面试登记-技能语言 服务实现类
    @Autowired
    private TbHrInterviewRegistrationLanguageService tbHrInterviewRegistrationLanguageService;

    // hr面试登记表-培训经历 服务实现类
    @Autowired
    private TbHrInterviewRegistrationTrainService tbHrInterviewRegistrationTrainService;

    // hr面试登记表-工作记录 服务实现类
    @Autowired
    private TbHrInterviewRegistrationWorkService tbHrInterviewRegistrationWorkService;


    /**
	 * 保存
	 * @author liuqinghua
	 * @date Sep 4, 2020 11:00:58 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbHrInterviewRegistration", allEntries=true)
    public boolean save(TbHrInterviewRegistration entity) {
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author liuqinghua
	 * @date Sep 4, 2020 11:00:58 AM
	 */
    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbHrInterviewRegistration getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Sep 4, 2020 11:00:58 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbHrInterviewRegistration", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 批量删除数据
	 * @author liuqinghua
	 * @date Sep 4, 2020 11:00:58 AM
	 */
    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

	/**
	 * 分页查询数据
	 * @author liuqinghua
	 * @date Sep 4, 2020 11:00:58 AM
	 */
    //@Cacheable(key = "#data",sync = true)
    public IPage<TbHrInterviewRegistration> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbHrInterviewRegistration entity = JSON.parseObject(JSON.toJSONString(data), TbHrInterviewRegistration.class);
        Page<TbHrInterviewRegistration> page = new Page(currentPage,pageSize);
        QueryWrapper<TbHrInterviewRegistration> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author liuqinghua
	 * @date Sep 4, 2020 11:00:58 AM
	 */
    @Override
    //@Cacheable(value = "TbHrInterviewRegistration",sync = true)
    public List<TbHrInterviewRegistration> selectAll() {
        return super.selectAll();
    }


    /**
     * hr面试记录登记表,保存
     * @author liuqinghua
     * @date Sep 4, 2020 11:00:58 AM
     */
    public void saveInterviewRegistration(SaveInterviewRegistrationDto data) throws Exception {
        // 保存hr面试记录登记表
        TbHrInterviewRegistration apply = data.getApply();
        if (!super.save(apply)) {
            throw new Exception("保存hr面试记录登记表异常");
        }
        // 保存hr面试登记表-教育经历
        List<TbHrInterviewRegistrationEducation> educationList = data.getEducationList();
        for (TbHrInterviewRegistrationEducation education : educationList) {
            education.setId(null);
            education.setInterviewRegistrationId(apply.getId());
            if (!tbHrInterviewRegistrationEducationService.save(education)) {
                throw new Exception("保存hr面试登记表-教育经历异常");
            }
        }
        // 保存hr面试登记表-家庭成员
        List<TbHrInterviewRegistrationFamily> familyList = data.getFamilyList();
        for (TbHrInterviewRegistrationFamily family : familyList) {
            family.setId(null);
            family.setInterviewRegistrationId(apply.getId());
            if (!tbHrInterviewRegistrationFamilyService.save(family)) {
                throw new Exception("保存hr面试登记表-家庭成员异常");
            }
        }
        // 保存hr面试登记表-文件
        List<TbHrInterviewRegistrationFile> resumeFileList = data.getResumeFileList();
        for (TbHrInterviewRegistrationFile resumeFile: resumeFileList) {
            resumeFile.setId(null);
            resumeFile.setInterviewRegistrationId(apply.getId());
            if (!tbHrInterviewRegistrationFileService.save(resumeFile)) {
                throw new Exception("保存hr面试登记表-文件异常");
            }
        }
        // 保存hr面试登记表-培训经历
        List<TbHrInterviewRegistrationTrain> trainList = data.getTrainList();
        for (TbHrInterviewRegistrationTrain train : trainList) {
            train.setId(null);
            train.setInterviewRegistrationId(apply.getId());
            if (!tbHrInterviewRegistrationTrainService.save(train)) {
                throw new Exception("保存hr面试登记表-培训经历异常");
            }
        }
        //  保存hr面试登记表-工作记录
        List<TbHrInterviewRegistrationWork> workList = data.getWorkList();
        for (TbHrInterviewRegistrationWork work : workList) {
            work.setId(null);
            work.setInterviewRegistrationId(apply.getId());
            if (!tbHrInterviewRegistrationWorkService.save(work)) {
                throw new Exception("保存hr面试登记表-工作记录异常");
            }
        }
        // 保存hr面试登记-技能语言
            TbHrInterviewRegistrationLanguageDto language = data.getLanguageList();
            List<String> languageNameList = language.getLanguage();
            List<Long> dictIdList = language.getDictId();
            for (String languageName: languageNameList) {
                int languageInt = languageNameList.indexOf(languageName);
                TbHrInterviewRegistrationLanguage tbHrInterviewRegistrationLanguage = new TbHrInterviewRegistrationLanguage();
                tbHrInterviewRegistrationLanguage.setInterviewRegistrationId(apply.getId());
                tbHrInterviewRegistrationLanguage.setLanguage(languageName);
                for (Long dictId: dictIdList) {
                    int dictInt = dictIdList.indexOf(dictId);
                    if (languageInt == dictInt) {
                        tbHrInterviewRegistrationLanguage.setDictId(dictId);
                        if (!tbHrInterviewRegistrationLanguageService.save(tbHrInterviewRegistrationLanguage)) {
                            throw new Exception("保存hr面试登记-技能语言异常");
                        }
                        break;
                    }

                }
            }



    }

    /**
     * hr面试记录登记表,修改
     * @author liuqinghua
     * @date Sep 4, 2020 11:00:58 AM
     */
    public void updataInterviewRegistration(SaveInterviewRegistrationDto data) throws Exception {
        TbHrInterviewRegistration apply = data.getApply();
        // hr面试记录登记相关删除
        this.deleteInterviewRegistrationCommon(apply.getId());
        // hr面试记录登记表,保存
        this.saveInterviewRegistration(data);
    }

    /**
     * hr面试记录登记表删除
     * @author liuqinghua
     * @date Sep 4, 2020 11:00:58 AM
     */
    public void deleteInterviewRegistration(Long id) throws Exception {
        // hr面试记录登记表删除
        int i = dao.deleteInterviewRegistration(id);
        if (i == 0) {
           throw new Exception("hr面试记录登记表删除异常");
        }
        // hr面试记录登记相关删除
        this.deleteInterviewRegistrationCommon(id);

    }

    /**
     * hr面试记录登记表删除公共
     * @author liuqinghua
     * @date Sep 4, 2020 11:00:58 AM
     */
    public void deleteInterviewRegistrationCommon(Long id) throws Exception {
        // 删除hr面试登记表-教育经历通过面试登记记录id
        tbHrInterviewRegistrationEducationService.deleteByInterviewRegistrationId(id);
        // 删除hr面试登记表-家庭成员通过面试登记记录id
        tbHrInterviewRegistrationFamilyService.deleteByInterviewRegistrationId(id);
        // 删除hr面试登记表-文件通过面试登记记录id
        tbHrInterviewRegistrationFileService.deleteByInterviewRegistrationId(id);
        // 删除hr面试登记表-培训经历通过面试登记记录id
        tbHrInterviewRegistrationTrainService.deleteByInterviewRegistrationId(id);
        // 删除hr面试登记表-工作记录通过面试登记记录id
        tbHrInterviewRegistrationWorkService.deleteByInterviewRegistrationId(id);
        // 删除hr面试登记-技能语言通过面试登记记录id
        tbHrInterviewRegistrationLanguageService.deleteByInterviewRegistrationId(id);
    }

    /**
     * hr面试记录登记表展示
     * @author liuqinghua
     * @date Sep 4, 2020 11:00:58 AM
     */
    public IPage<ShowInterviewRegistrationQuery> showInterviewRegistration(ShowInterviewRegistrationDto data) {
        Page<ShowInterviewRegistrationQuery> page = new Page(data.getPage(),data.getSize());
     return dao.showInterviewRegistration(page,data);
    }

    /**
     * hr面试记录登记表具体数据
     * @author liuqinghua
     * @date Sep 4, 2020 11:00:58 AM
     */
    public ShowInterviewRegistrationDetailQuery showInterviewRegistrationDetail(Long id) {
        ShowInterviewRegistrationDetailQuery show= new ShowInterviewRegistrationDetailQuery();
        // 获取hr面试记录登记表基本数据
        TbHrInterviewRegistration tbHrInterviewRegistration = dao.selectById(id);
        if (tbHrInterviewRegistration != null) {
        // hr面试登记表-教育经历
        QueryWrapper<TbHrInterviewRegistrationEducation> queryWrapper = new QueryWrapper();
        queryWrapper.eq("interview_registration_id",id);
        List<TbHrInterviewRegistrationEducation> educationList = tbHrInterviewRegistrationEducationService.list(queryWrapper);
        // hr面试登记表-家庭成员
        QueryWrapper<TbHrInterviewRegistrationFamily> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("interview_registration_id",id);
        List<TbHrInterviewRegistrationFamily> familyList = tbHrInterviewRegistrationFamilyService.list(queryWrapper1);
        // hr面试登记表-培训经历
        QueryWrapper<TbHrInterviewRegistrationTrain> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("interview_registration_id",id);
        List<TbHrInterviewRegistrationTrain> trainList = tbHrInterviewRegistrationTrainService.list(queryWrapper2);

        // hr面试登记表-工作记录
        QueryWrapper<TbHrInterviewRegistrationWork> queryWrapper3 = new QueryWrapper();
        queryWrapper3.eq("interview_registration_id",id);
        List<TbHrInterviewRegistrationWork> workList = tbHrInterviewRegistrationWorkService.list(queryWrapper3);

        // hr面试登记表-文件
        QueryWrapper<TbHrInterviewRegistrationFile> queryWrapper4 = new QueryWrapper();
        queryWrapper4.eq("interview_registration_id",id);
        List<TbHrInterviewRegistrationFile> resumeFileList = tbHrInterviewRegistrationFileService.list(queryWrapper4);

        // hr面试登记-技能语言
        TbHrInterviewRegistrationLanguageQuery tbHrInterviewRegistrationLanguageQuery = new TbHrInterviewRegistrationLanguageQuery();
            List<Long> dictIdQuery = new ArrayList<>();
            List<String> languageQuery =  new ArrayList<>();
            QueryWrapper<TbHrInterviewRegistrationLanguage> queryWrapper5 = new QueryWrapper();
            queryWrapper5.eq("interview_registration_id",id);
           List<TbHrInterviewRegistrationLanguage> languageList = tbHrInterviewRegistrationLanguageService.list(queryWrapper5);
            for (TbHrInterviewRegistrationLanguage language: languageList) {
                dictIdQuery.add(language.getDictId());
                languageQuery.add(language.getLanguage());
            }
            tbHrInterviewRegistrationLanguageQuery.setDictId(dictIdQuery);
            tbHrInterviewRegistrationLanguageQuery.setLanguage(languageQuery);

        show.setApply(tbHrInterviewRegistration);
        show.setEducationList(educationList);
        show.setFamilyList(familyList);
        show.setLanguageList(tbHrInterviewRegistrationLanguageQuery);
        show.setResumeFileList(resumeFileList);
        show.setTrainList(trainList);
        show.setWorkList(workList);
        }
        return show;
    }

    /**
     * 查询每月面试人数
     * @author 苏小林
     * @date 2020/9/14 14:55
     * @param: date
     * @return: java.util.List<java.lang.Integer>
     */
    public int[] getCountByDate(String date) {

        Calendar calendar = Calendar.getInstance();
        //获得当前年
        int year=calendar.get(Calendar.YEAR);

        //获得当前时bai间的月份，月份从0开始所以结果要加1
        int month=calendar.get(Calendar.MONTH)+1;
        //定义数组
        int[] countVIP = new int[month];

        if (StringUtils.isBlank(date)) {
            date = year +"";
        } else {
            if (!date.equals(year+"")) {
                countVIP = new int[12];
            }
        }

        List<MonthCountVo> list = dao.getCountByDate(date);
        // 判断当月时间
        String year1 = String.valueOf(LocalDate.now().getYear());
        MonthCountVo cunrrentCountByDate = null;
        if(year1.equals(date))
            cunrrentCountByDate = dao.getCunrrentCountByDate();
        if (list.size() >0 && list != null) {
            for (int i = 0; i < list.size(); i++) {
                MonthCountVo monthCountVo = list.get(i);
                if (monthCountVo != null) {
                    if(cunrrentCountByDate != null && cunrrentCountByDate.getMonth() == monthCountVo.getMonth()){
                        monthCountVo.setCount(cunrrentCountByDate.getCount());
                    }
                    //填充月份对应的数据
                    countVIP[monthCountVo.getMonth()-1] = monthCountVo.getCount();
                }

            }
        }
        return countVIP;
    }
}
